package tech.xs.dao.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;

import tech.xs.dao.EmployeeDao;
import tech.xs.pojo.Employee;
@Repository
public class EmployeeDaoImpl implements EmployeeDao{
	@Autowired
	private HibernateTemplate hibernateTemplate;

	@SuppressWarnings({ "unchecked", "deprecation" })
	@Override
	public Employee login(Employee employee) {
		// TODO Auto-generated method stub
		String hql = "from Employee where sn=?0 and password=?1";
		List<Employee> list = (List<Employee>) hibernateTemplate.find(hql,employee.getSn(),employee.getPassword());
		if(null!=list&&list.size()>0){
			return list.get(0);
		}
		return null;
	}

	@SuppressWarnings("deprecation")
	@Override
	public Employee findNext(Employee employee) {
		// TODO Auto-generated method stub
		Employee emp = (Employee) hibernateTemplate.find("from Employee where position.id = ?0 and department.id = ?1",employee.getSysPosition().getId(),employee.getSysDepartment().getId());    
        return emp;
	}

	@SuppressWarnings({ "unchecked", "deprecation" })
	@Override
	public Employee findById(String id) {
		// TODO Auto-generated method stub
		List<Employee> list =  (List<Employee>) hibernateTemplate.find("from Employee where sn = ?0",id);
		System.out.println(list.size());
		return list.get(0);
	}

	@SuppressWarnings({ "unchecked", "deprecation" })
	@Override
	public Employee findEmployee(Employee employee) {
		// TODO Auto-generated method stub
		String hql="from Employee e ";
        if(employee.getSysPosition().getId()==1){
            hql+=" where e.sysDepartment.id="+employee.getSysDepartment().getId()+" and e.sysPosition.id=2 and e.status='在职'";
        }else if(employee.getSysPosition().getId()==2){
            hql+=" where e.sysPosition.id=3 and e.status='在职'";
        }else if(employee.getSysPosition().getId()==3){
            hql+=" where e.sysPosition.id=4 and e.status='在职'";
        }
        List<Employee> list =  (List<Employee>) hibernateTemplate.find(hql);
        return  list.get(0);
	}
	

	
	
}
